package task.manager;

import java.util.HashMap;
import java.util.Map;
import java.io.*;

import task.manager.AccountView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * The main activity class for the task manager.
 * 
 * @author Harrison Saylor, Aaron Mead, Marc Huet, Greg Ruskin
 * @version 1.0
 */
public class TaskManagerActivity extends Activity {
	
	// Username/password
	private EditText etUsername;
    private EditText etPassword;
    
    // Buttons.
    private Button btnLogin;
    private Button btnCancel;
    private Button btnRegister;
    
    // Result label.
    private TextView lblResult;
    
    private LoginFacade loginFacade;
    
	/** 
	 * Called when the activity is started. 
	 * 
	 * @param savedInstanceState Any saved instance state.
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        loginFacade = LoginFacade.getInstance();
        System.out.println("I made it!!");
        
        // Instantiate variables.
        etUsername = (EditText)findViewById(R.id.username);
        etPassword = (EditText)findViewById(R.id.password);
        btnLogin = (Button)findViewById(R.id.login_button);
        btnCancel = (Button)findViewById(R.id.cancel_button);
        btnRegister = (Button)findViewById(R.id.register_button);
        lblResult = (TextView)findViewById(R.id.result);
        
        System.out.println("here");
        
        /**
         * Binding Click event to login button.
         */
        btnLogin.setOnClickListener(new OnClickListener() {
        	/**
        	 * Response to clicking login.
        	 */
        	public void onClick(View v) {
		  		// Check Login
		  		String username = etUsername.getText().toString();
		  		String password = etPassword.getText().toString();
	
		  		if(loginFacade.isValid(username, password)){ // If username and password match and is valid.
		  			lblResult.setText("Login successful.");
		  			loginFacade.setActiveUser(username);
		  			Intent categories = new Intent(getApplicationContext(), CategoryViewActivity.class);
	        		startActivity(categories);
		  		} else {
		  			lblResult.setText("Login failed. Incorrect username and/or password.");
		  		}
        	} // End of onClick method.
        }); // End of setOnClickListener.
        
        
        
        /**
         * Binding Click event to cancel button.
         */
        btnCancel.setOnClickListener(new OnClickListener() {
        	/**
        	 * Response to clicking cancel.
        	 */
		  	public void onClick(View v) {
		  		finish(); // Closes the application.
		  	} // End of onClick method.
        }); // End of setOnClickListener.
        
        /**
         * Binding Click event to register the user.
         */
        btnRegister.setOnClickListener(new OnClickListener() {
        	/**
        	 * Response to clicking register.
        	 */
        	public void onClick(View v) {
        		// Goes to account creation screen.
        		Intent nextScreen = new Intent(getApplicationContext(), AccountView.class);
        		startActivity(nextScreen);
        	} // End of onClick method.
        }); // End of setOnClickListener.
          
    } // End of onCreate method.
    
} // End of TaskManagerActivity class.

    
